using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Security;
using SJKP.PDFConversion.Client;
using SJKP.PDFConversion.Service;

namespace SJKP.PDFConversion.Features.InstallOCRService
{
    /// <summary>
    /// Install the OCR Service application.
    /// </summary>
    [Guid("af1c866f-69c4-4c15-ba22-485a29cea446")]
    public class InstallOCRServiceEventReceiver : SPFeatureReceiver
    {

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebService webService = properties.Feature.Parent as SPWebService;
            if (webService == null)
                throw new ArgumentException("Not activated at farm scope");

            SPFarm spFarm = SPFarm.Local;
            OCRService service = spFarm.Services.GetValue<OCRService>();
            if (service == null)
            {
                //install service
                service = new OCRService(spFarm);
                service.Update();
            }
            
            if (service.Status != SPObjectStatus.Online)
            {
                service.Status = SPObjectStatus.Online;
                service.Update();
                service.Provision();
            }

            OCRServiceInstance instance = SPServer.Local.ServiceInstances.GetValue<OCRServiceInstance>();
            if (instance == null)
            {
                //install service instance
                instance = new OCRServiceInstance(SPServer.Local, service);
                instance.Update(true);
            }
            //Install service proxy
            OCRServiceProxy serviceProxy = spFarm.ServiceProxies.GetValue<OCRServiceProxy>();
            if (serviceProxy == null)
            {
                serviceProxy = new OCRServiceProxy(spFarm);
                serviceProxy.Update();
            }

        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebService webService = properties.Feature.Parent as SPWebService;
            if (webService == null)
                throw new ArgumentException("Not activated at farm scope");
            
            OCRServiceInstance instance = SPServer.Local.ServiceInstances.GetValue<OCRServiceInstance>();
            if (instance != null)
                instance.Delete();

            SPServer.Local.Update();
            OCRServiceProxy serviceProxy = webService.Farm.ServiceProxies.GetValue<OCRServiceProxy>();
            if (serviceProxy != null)
            {
                serviceProxy.Delete();
            }

            

            OCRService service = webService.Farm.Services.GetValue<OCRService>();
            if (service != null)
                service.Delete();
            webService.Farm.Update();
            
        }
    }
}
